package base.test;

import base.utils.ArrayUitls;
import base.utils.PrintUtils;

import java.util.Arrays;

public class Demo {


    public static boolean isStartRepeat(int flag) {
        return (flag & 2) == 2;
    }


    public static boolean isEndRepeat(int flag) {
        return (flag & 4) == 4;
    }

    public static void main(String[] args) {
//        int[] result = mergeSort(arrList);
//
//        for (int item  : result){
//            System.out.println(item);
//        }


        if (args.length < 1){
            System.out.println("args size == 0 ");
        }


        for(int i = 0 ; i < args.length ; i++){
            System.out.println(args[i]);
        }

    }

    private static int[] arrList = {5,9,4,3,1,6,8,9 , 55,99,10};


    public static void bubbleSort(int[] arr){
        int arrSize = arr.length;
        for (int i = arrSize - 1 ; i >= 0  ; i--){
            for (int j = 0 ; j < i ; j++){
                int tmp =arr[j];
                if (arr[j] > arr[j+1]){
                    tmp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = tmp;
                }
            }
        }

    }


    public static void selectSort(int[] arr) {
        int arrSize = arr.length;

        for (int i =0 ;i < arrSize - 1 ; i++){
            int min = i ;

            for (int j = i + 1; j <= arrSize - 1; j++) {
                if (arr[min] > arr[j]) {
                    min = j;
                }
            }

            if (min != i){
                int tmp = arr[min];
                arr[min] =   arr[i];
                arr[i] = tmp;
            }

        }

    }


   public static void inserttSort(int[] arr) {
        int arrSize = arr.length;

        for (int i = 1 ;i < arrSize ; i++){

            int tmp = i;

            while (tmp>0 && (arr[tmp-1] > arr[tmp])){
                int var = arr[tmp];
                arr[tmp] = arr[tmp-1];
                arr[tmp-1] = var;
                tmp--;
            }
        }


    }


    public static void shellSort(int[] arr) {
        int gap = arr.length;
        int size = arr.length;

        while (true){
            gap = gap / 2 ;
            for (int i = 0; i <= gap;i++){

                for (int j = i;j<size; j+=gap){
                    int tmp = j;
                    int a = arr[j];
                    while (tmp >= gap && (a < arr[tmp - gap])) {
                        arr[tmp] = arr[tmp - gap];
                        arr[tmp - gap] = a;
                        tmp -= gap;
                    }
                }

            }
            if (gap==1) break;
        }



    }


    public static int[] mergeSort(int[] arr) {
        int gap = arr.length;
        int size = arr.length;

        if (arr.length<2){
            return arr;
        }

        int middle = arr.length / 2;

        int[] left = Arrays.copyOfRange(arr , 0 , middle);
        int[] right = Arrays.copyOfRange(arr , middle,arr.length);

        return mergeArr(mergeSort(left),mergeSort(right));
    }

    private static int[] merge(int[] left, int[] right) {
        int[] result = new int[left.length + right.length];
        int i = 0;

        while (left.length > 0 && right.length > 0) {
            if (left[0] >= right[0]) {
                result[i] = right[0];
                right = Arrays.copyOfRange(right, 1, right.length);
            } else {
                result[i] = left[0];
                left = Arrays.copyOfRange(left, 1, left.length);
            }
            i++;
        }

        while (left.length > 0) {
            result[i] = left[0];
            left = Arrays.copyOfRange(left, 1, left.length);
            i++;
        }

        while (right.length > 0) {
            result[i] = right[0];
            right = Arrays.copyOfRange(right, 1, right.length);
            i++;
        }

        return result;
    }

    private static int[] mergeArr(int[] left, int[] right) {
        int[] result = new int[left.length + right.length];
        int i = 0;

        int a =0 ,b =0;


        // 并 对两个 (已经排序)数组 ， 依据大小进行排序，这里的极限是两个length为1 数组进行比较 ,或者 left={4,};right={1,3,}
        while (a < left.length && b < right.length ){
            if (left[a] > right[b]){
                result[i] = right[b];
                b++;
            }else {
                result[i]  = left [a];
                a++;
            }
            i++;
        }

        while (left.length > a) {
            result[i]  = left [a];
            a++;
            i++;
        }

        while (right.length > b) {
            result[i] = right[b];
            b++;
            i++;
        }

        PrintUtils.print("left="+ ArrayUitls.arr2String(left)+
                ";right="+ArrayUitls.arr2String(right)+
                ";result="+ArrayUitls.arr2String(result));
        return result;
    }


    public static void text(){

        int zero = 0;
        int one = 1;
        int two = 2;
        int four = 4;
        int six = 6;

        int[]array={0,1,2,4,6};

        for (int item :array){

            System.out.println(item + "  ;  isstart = "+isStartRepeat(item)+" ; end = "+isEndRepeat(item));

        }

    }








}













